"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const electron_updater_1 = require("electron-updater");
const electron_1 = require("electron");
const config = require('./config');

const GLOBAL_APP_NAME = config.GLOBAL_APP_NAME;
//update-checking，update-checked,update-downloading，update-downloaded,update-error
class updater {
    /**
     *
     */
    constructor(url) {
        this.autoUpdater = url;

        this.checkInit();
        this.ipcInit();
        // this.checkUpdates = this.checkUpdates.bind(this);
        // this.install = this.install.bind(this);
        // this.download = this.download.bind(this);
    }
    checkUpdates() {
        this.send('update-checking');
        electron_updater_1.autoUpdater.checkForUpdates();
    }
    setWindow(window) {
        this.window = window;
    }
    checkInit() {

        console.log(this.autoUpdater);
        electron_updater_1.autoUpdater.setFeedURL(this.autoUpdater);
        electron_updater_1.autoUpdater.autoDownload = false;
        // electron_updater_1.autoUpdater.autoInstallOnAppQuit = false;
        electron_updater_1.autoUpdater.on('update-available', ui => {
            this.available = true;
            const n = new electron_1.Notification({
                title: GLOBAL_APP_NAME,
                body: `发现新版本 ${ui.version}，点击下载更新。`,
            });
            n.on('click', () => {
                this.download();
            });
            n.show();
            console.log('update info:', ui);
            this.send('update-checked', true, ui.version);
        });
        electron_updater_1.autoUpdater.on('update-not-available', () => {
            this.send('update-checked', false);
        });
        electron_updater_1.autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
            const n = new electron_1.Notification({
                title: `${GLOBAL_APP_NAME} ${event.version} 更新`,
                body: `更新已下载完成，开始安装更新。`,
            });
            this.send('update-downloaded');
            this.isDownloaded = true;
            n.on('click', () => {
                electron_updater_1.autoUpdater.quitAndInstall();
            });

            n.show();
        });
        electron_updater_1.autoUpdater.on('error', (event) => {
            this.isDownload = false;
            this.isDownloaded = false;
            this.send('update-error');
        });

        electron_updater_1.autoUpdater.on('download-progress', (event, info) => {
            this.send('update-download-progress', event, info);
        });
    }
    ipcInit() {
        electron_1.ipcMain.on('update-check', () => this.checkUpdates());
        electron_1.ipcMain.on('update-install', () => this.install());
        electron_1.ipcMain.on('update-download', () => this.download());
    }
    removeIpc() {
        electron_1.ipcMain.removeAllListeners('update-check');
        electron_1.ipcMain.removeAllListeners('update-install');
        electron_1.ipcMain.removeAllListeners('update-download');
    }
    install() {
        electron_updater_1.autoUpdater.quitAndInstall();
    }

    download() {
        this.send('update-downloading');
        this.isDownload = true;
        return electron_updater_1.autoUpdater.downloadUpdate();
    }
    send(channel, ...args) {
        const window = electron_1.BrowserWindow.getAllWindows()[0];
        console.log(channel);
        window && window.webContents.send(channel, ...args);
    }

    destroy() {
        this.window = null;
        this.removeIpc();
    }
}
exports.updater = updater;